// pages/charge/index.js
Page({
  data: {
    payType: [{
      money: '30元',
      give: '3000书币',
      other: '多送0元',
      shubi:3000
    }, {
        money: '50元',
        give: '5000+3000书币',
        other: '多送30元',
        shubi: 8000
      }, {
        money: '100元',
        give: '10000+8000书币',
        other: '多送80元',
        shubi: 18000
    }, {
      money: '200元',
      give: '20000+20000书币',
      other: '多送200元',
      shubi: 40000
      }, {
        money: '年费VIP会员(365元)',
        give: '每天1元，全年免费看',
        other: ''
      }],
    inputValue: 0,
    curIndex:0,
    overage:0,
    shubi:0
  },
  // 页面加载
  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: '充值'
    })
  },
  // 存储输入的充值金额
  bindInput: function (res) {
    this.setData({
      inputValue: res.detail.value
    })
  },
  selectType: function (e) {
    var that = this;
    let index = parseInt(e.target.dataset.index);
    let shubi = parseInt(e.target.dataset.shubi);
    that.setData({
      curIndex: index,
      shubi: shubi,
    })
  },
  // 充值
  charge: function () {
    wx.redirectTo({
      url: '../user/user',
      success: function (res) {
        wx.showToast({
          title: "充值成功",
          icon: "success",
          duration: 2000
        })
      }
    })
  },
  // 页面销毁，更新本地金额，（累加）
  onUnload: function () {
    wx.getStorage({
      key: 'overage',
      success: (res) => {
        wx.setStorage({
          key: 'overage',
          data: {
            overage: parseInt(this.data.inputValue) + parseInt(res.data.overage)
          }
        })
      },
      fail: (res) => {
        wx.setStorage({
          key: 'overage',
          data: {
            overage: parseInt(this.data.inputValue)
          },
        })
      }
    })
  }
})